<?php

namespace app\api\controller;

use app\MyController;
use app\common\model\User;
use app\common\model\UserAccount;
use think\facade\Request;
use app\common\model\User as UserModel;
header('Access-Control-Allow-Origin: *');

/**
 * 商户后台控制器基类
 */
class Base extends MyController
{
    public $page = '10';

    /**
     * 获取当前用户信息
     */
    protected function getUser($falg = true)
    {
        $token = request()->header('token', '');
        // if (empty($token)) {
        //     $token = '6799826bc2100fe8ebaf8b40ade27f77743cd6d9';
        // }
        $user_account = UserAccount::getInfo(['account_token' => $token]);
        if (!$user_account) {
            if ($falg) {
                $data['status'] = '-1';
                $data['msg'] = "没有找到用户信息1";
                exit(json_encode($data, 1));
            }
            return false;
        }

        // $user_account_is = user_md5($user_account['user_id'] . $user_account['token_num'] . $user_account['token_time']);
        // if ($token !== $user_account_is) {
        //     $data['status'] = '-1';
        //     $data['msg'] = "没有找到用户信息2";
        //     exit(json_encode($data, 1));
        // }

        $user = User::where(['id' => $user_account['user_id']])->find();
        if (!$user) {
            $data['status'] = '-1';
            $data['msg'] = "没有找到用户信息3";
            exit(json_encode($data, 1));
        }
        if ($user['status'] != 1) {
            $data['status'] = '-1';
            $data['msg'] = "该账户已被封号，请联系客服解封";
            exit(json_encode($data, 1));
        }
        return $user;
    }

    /**
     * 获取当前用户信息
     */
    protected function getuserid()
    {
        $user_id = 0;
        $token = request()->header('token', '');
        $user_account = UserAccount::getInfo(['account_token' => $token]);
        if ($user_account) {
            $user = User::where(['id' => $user_account['user_id']])->find();
            if ($user && $user['status'] == 1) {
                $user_id = $user['id'];
            }
        }
        return $user_id;
    }
    
     /*
     * 判断优惠券是否领过
     */
     protected function getUserid1($is_force = true)
    {
        $header = Request::header();
        if($header['token']==''){
            $token = $this->request->param('token');
        }else{
            $token = $header['token'];
        }
        $header['logintype'] = 1;
        if(!isset($header['logintype']) or !in_array($header['logintype'],[1,2])) {
           return false;
        }
        if (!$token) {
            if ($is_force) {
                 return false;
            }
           
        }
        $user_account = UserAccount::getInfo(['account_token'=>$token]);
        if(empty($user_account)){
            if($is_force){
                return false;
            }
            return  false;
        }
        $user = UserModel::getInfo(['id'=>$user_account['user_id']]);
        if(empty($user)){
            return false;
        }
        if ($user['status'] != 1) {
            return false;
        }
        #判断优惠券是否领过
        if($user['is_use_coupon'] == 1){
            return false;
        }else{
            return true;
        }
    }


}
